<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="污染指数" prop="aqi">
        <el-input
          v-model="queryParams.aqi"
          placeholder="请输入AQI"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入城市名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期" prop="ct">
        <el-date-picker clearable
          v-model="queryParams.ct"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始日期" prop="start">
        <el-date-picker clearable
          v-model="queryParams.start"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['avg:pollutant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['avg:pollutant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['avg:pollutant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['avg:pollutant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pollutantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="AQI" align="center" prop="aqi" />
      <el-table-column label="PM10" align="center" prop="pm10" />
      <el-table-column label="PM25" align="center" prop="pm25" />
      <el-table-column label="SO2" align="center" prop="so2" />
      <el-table-column label="NO2" align="center" prop="no2" />
      <el-table-column label="CO" align="center" prop="co" />
      <el-table-column label="O3" align="center" prop="o3" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
        <el-tag :type="scope.row.type=== 1 ? 'success' : (scope.row.type=== 2 ? '':(scope.row.type=== 3 ? 'warning':'danger'))"> {{scope.row.type=== 1 ? '日均值' : (scope.row.type=== 2 ? '周均值':(scope.row.type=== 3 ? '月均值':'年均值')) }}  </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="城市" align="center" prop="area" />
      <el-table-column label="日期" align="center" prop="ct" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ct, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始" align="center" prop="start" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.start, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束" align="center" prop="end" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.end, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column  fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['avg:pollutant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['avg:pollutant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination 
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改均值查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="AQI" prop="aqi">
          <el-input v-model="form.aqi" placeholder="请输入AQI" />
        </el-form-item>
        <el-form-item label="type" prop="type">
          <el-select v-model="value"  placeholder="" >
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
      >
    </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="PM10" prop="pm10">
          <el-input v-model="form.pm10" placeholder="请输入PM10" />
        </el-form-item>
        <el-form-item label="PM2.5" prop="pm25">
          <el-input v-model="form.pm25" placeholder="请输入PM2.5" />
        </el-form-item>
        <el-form-item label="SO2" prop="so2">
          <el-input v-model="form.so2" placeholder="请输入SO2" />
        </el-form-item>
        <el-form-item label="NO2" prop="no2">
          <el-input v-model="form.no2" placeholder="请输入NO2" />
        </el-form-item>
        <el-form-item label="CO" prop="co">
          <el-input v-model="form.co" placeholder="请输入CO" />
        </el-form-item>
        <el-form-item label="O3" prop="o3">
          <el-input v-model="form.o3" placeholder="请输入O3" />
        </el-form-item>
        <el-form-item label="城市" prop="area">
          <el-input v-model="form.area" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="日期" prop="ct">
          <el-date-picker clearable
            v-model="form.ct"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始日期" prop="start">
          <el-date-picker clearable
            v-model="form.start"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择"
           >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="end">
          <el-date-picker clearable
            v-model="form.end"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择"
            >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPollutant, getPollutant, delPollutant, addPollutant, updatePollutant } from "@/api/avg/pollutant";

export default {
  name: "Pollutant",
  data() {
    return {
      value: '',
      pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        },
      options: [{
          value: '1',
          label: '日均值'
        }, {
          value: '2',
          label: '周均值'
        }, {
          value: '3',
          label: '月均值'
        }, {
          value: '4',
          label: '年均值'
        }],
        value: '',
        t:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 均值查询表格数据
      pollutantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        aqi: null,
        pm10: null,
        pm25: null,
        so2: null,
        no2: null,
        co: null,
        o3: null,
        type: null,
        area: null,
        ct: null,
        start: null,
        end: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询均值查询列表 */
    getList() {
      this.loading = true;
      listPollutant(this.queryParams).then(response => {
        this.pollutantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        aqi: null,
        pm10: null,
        pm25: null,
        so2: null,
        no2: null,
        co: null,
        o3: null,
        type: null,
        area: null,
        ct: null,
        start: null,
        end: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加均值查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPollutant(id).then(response => {
        this.form = response.data;
         this.t = this.form.type
        if(this.t==1) this.value = '1'
        else if(this.t==2) this.value = '2'
        else if(this.t==3) this.value = '3'
        else if(this.t==4) this.value = '4'
        this.open = true;
        this.title = "修改均值查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePollutant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPollutant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除均值查询编号为"' + ids + '"的数据项？').then(function() {
        return delPollutant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('avg/pollutant/export', {
        ...this.queryParams
      }, `pollutant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
